package com.server.pinggu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.server.common.utils.EasyExcelUtils;
import com.server.common.utils.StringUtils;
import com.server.common.utils.upload.UploadFile;
import com.server.pinggu.domain.SenActionlog;
import com.server.pinggu.domain.SenForms;
import com.server.pinggu.domain.SenFormsRemark;
import com.server.pinggu.domain.SenPgy;
import com.server.pinggu.dto.PageValue;
import com.server.pinggu.excel.FormsSendOrdersDataHandle;
import com.server.pinggu.excel.FormsUpdateLevelDataHandle;
import com.server.pinggu.excel.InitStockDataHandle;
import com.server.pinggu.mapper.*;
import com.server.pinggu.service.ISenFormsService;
import com.server.pinggu.upload.*;
import com.server.pinggu.vo.SendOrderVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ltao
 * @date 2022-10-23
 */
@Service
public class SenFormsServiceImpl extends ServiceImpl<SenFormsMapper, SenForms> implements ISenFormsService {

    @Resource
    private SenActionlogMapper senActionlogMapper;

    @Resource
    private SenFormsRemarkMapper senFormsRemarkMapper;

    @Resource
    private SenStreetsMapper senStreetsMapper;

    @Resource
    private SenPgyMapper senPgyMapper;

    @Resource
    private SenFormsMapper senFormsMapper;


    private final Map<Long, String> value = new HashMap<Long, String>() {{
        put(12456L, "居家信息");
        put(12451L, "居家信息");
        put(12452L, "居家信息");
        put(11663L, "居家信息");
        put(11664L, "居家信息");
        put(11665L, "居家信息");
        put(11666L, "居家信息");
        put(11667L, "居家信息");
        put(11668L, "居家信息");
        put(11673L, "居家信息");
        put(11674L, "居家信息");
        put(11675L, "居家信息");
        put(11678L, "居家信息");
        put(11679L, "居家信息");
        put(11680L, "居家信息");
        put(11681L, "居家信息");
        put(11682L, "居家信息");
        put(11683L, "居家信息");
        put(11684L, "居家信息");
        put(11685L, "居家信息");
        put(11686L, "居家信息");
        put(11687L, "居家信息");
        put(11688L, "居家信息");
        put(11689L, "居家信息");
        put(12431L, "居家信息");
        put(12433L, "居家信息");
        put(11691L, "居家信息");
        put(11693L, "居家信息");
        put(11694L, "居家信息");
        put(11695L, "居家信息");
        put(11696L, "居家信息");
        put(11700L, "居家信息");
        put(12438L, "居家信息");
        put(12440L, "居家信息");
        put(12441L, "居家信息");
        put(12442L, "居家信息");
        put(11701L, "居家信息");
        put(11702L, "居家信息");
        put(11703L, "居家信息");
        put(11704L, "居家信息");
        put(11705L, "居家信息");
        put(11706L, "居家信息");
        put(11690L, "居家信息");
        put(12432L, "居家信息");
        put(12435L, "居家信息");
        put(11692L, "居家信息");
        put(12453L, "居家信息");
        put(12437L, "居家信息");
        put(11697L, "居家信息");
        put(12439L, "居家信息");
        put(12436L, "居家信息");
        put(11707L, "居家信息");
        put(12443L, "居家信息");
        put(11710L, "居家信息");
        put(11711L, "居家信息");
        put(11712L, "居家信息");
        put(11713L, "居家信息");
        put(11714L, "居家信息");
        put(12444L, "居家信息");
        put(12445L, "居家信息");
        put(12446L, "居家信息");
        put(12447L, "居家信息");
        put(12448L, "居家信息");
        put(12449L, "居家信息");
        put(12450L, "居家信息");
        put(12071L, "总体状况");
        put(12072L, "总体状况");
        put(12073L, "总体状况");
        put(12074L, "总体状况");
        put(12075L, "总体状况");
        put(12034L, "基本信息");
        put(12035L, "基本信息");
        put(12033L, "基本信息");
        put(12032L, "基本信息");
        put(12031L, "基本信息");
        put(12030L, "基本信息");
        put(12029L, "基本信息");
        put(12028L, "基本信息");
        put(12027L, "基本信息");
        put(12026L, "基本信息");
        put(12025L, "基本信息");
        put(12024L, "基本信息");
        put(12023L, "基本信息");
        put(12022L, "基本信息");
        put(12021L, "基本信息");
        put(12020L, "基本信息");
        put(12019L, "基本信息");
        put(12018L, "基本信息");
        put(12017L, "基本信息");
        put(12036L, "基本信息");
        put(12037L, "基本信息");
        put(12038L, "基本信息");
        put(12039L, "基本信息");
        put(12040L, "基本信息");
        put(12041L, "基本信息");
        put(12042L, "基本信息");
        put(12043L, "基本信息");
        put(12044L, "基本信息");
        put(12045L, "基本信息");
        put(12046L, "基本信息");
        put(12047L, "基本信息");
        put(12048L, "基本信息");
        put(12049L, "基本信息");
        put(12050L, "基本信息");
        put(12051L, "基本信息");
        put(12052L, "基本信息");
        put(12053L, "基本信息");
        put(12054L, "基本信息");
        put(12055L, "基本信息");
        put(12056L, "基本信息");
        put(12057L, "基本信息");
        put(12058L, "基本信息");
        put(12059L, "基本信息");
        put(12060L, "基本信息");
        put(12061L, "基本信息");
        put(12062L, "基本信息");
        put(12063L, "基本信息");
        put(12064L, "基本信息");
        put(12065L, "基本信息");
        put(12066L, "基本信息");
        put(12067L, "基本信息");
        put(12068L, "基本信息");
        put(12069L, "基本信息");
        put(12070L, "基本信息");
        put(12455L, "基本信息");

        put(12076L, "疾病总体信息");
        put(12077L, "疾病总体信息");
        put(12078L, "疾病总体信息");
        put(12079L, "疾病总体信息");
        put(12080L, "疾病总体信息");
        put(12081L, "疾病总体信息");
        put(12083L, "疾病总体信息");
        put(12084L, "疾病总体信息");
        put(12085L, "疾病总体信息");
        put(12086L, "疾病总体信息");
        put(12087L, "疾病总体信息");
        put(12088L, "疾病总体信息");
        put(12089L, "疾病总体信息");
        put(12090L, "疾病总体信息");
        put(12091L, "疾病总体信息");
        put(12092L, "疾病总体信息");
        put(12093L, "疾病总体信息");
        put(12094L, "疾病总体信息");
        put(12095L, "疾病总体信息");
        put(12096L, "疾病总体信息");
        put(12097L, "疾病总体信息");
        put(12098L, "疾病总体信息");
        put(12099L, "疾病总体信息");
        put(12100L, "疾病总体信息");
        put(12101L, "疾病总体信息");
        put(12103L, "疾病总体信息");
    }};

    private final Map<Long, String> xuhao = new HashMap<Long, String>() {{
        put(12456L, "第一项");
        put(12451L, "第二项");
        put(12452L, "第三项");
        put(11663L, "第四项");
        put(11664L, "第五项");
        put(11665L, "第六项");
        put(11666L, "第七项");
        put(11667L, "第八项");
        put(11668L, "第九项");
        put(11673L, "第十项");
        put(11674L, "第十一项");
        put(11675L, "第十二项");
        put(11687L, "第十四项");
        put(11688L, "第十五项");
        put(12431L, "第十六项");
        put(12433L, "第十七项");
        put(11691L, "第十八项");
        put(11693L, "第十九项");
        put(11694L, "第二十项");
        put(11695L, "第二十一项");
        put(11696L, "第二十二项");
        put(11700L, "第二十三项");
        put(12438L, "第二十四项");
        put(12440L, "第二十五项");
        put(12441L, "第二十六项");
        put(12442L, "第二十七项");
        put(11703L, "第二十九项");
        put(11704L, "第三十项");
        put(11705L, "第三十一项");
        put(11706L, "第三十二项");
        put(11707L, "第三十三项");
        put(12443L, "第三十四项");
        put(11710L, "第三十五项");
        put(11711L, "第三十六项");


        put(12017L, "第一项");
        put(12018L, "第二项");
        put(12019L, "第三项");
        put(12020L, "第四项");
        put(12021L, "第五项");
        put(12022L, "第六项");
        put(12023L, "第七项");
        put(12024L, "第八项");
        put(12025L, "第九项");
        put(12026L, "第十项");
        put(12027L, "第十一项");
        put(12028L, "第十二项");
        put(12029L, "第十三项");
        put(12030L, "第十四项");
        put(12031L, "第十五项");
        put(12033L, "第十六项");
        put(12035L, "第十七项");
        put(12036L, "第十八项");
        put(12038L, "第十九项");
        put(12039L, "第二十项");
        put(12040L, "第二十一项");
        put(12041L, "第二十二项");
        put(12042L, "第二十三项");
        put(12043L, "第二十四项");
        put(12044L, "第二十五项");
        put(12045L, "第二十六项");
        put(12046L, "第二十七项");
        put(12047L, "第二十八项");

        put(12048L, "第二十九项");
        put(12049L, "第三十项");
        put(12050L, "第三十一项");
        put(12051L, "第三十二项");
        put(12052L, "第三十三项");
        put(12053L, "第三十四项");
        put(12054L, "第三十五项");
        put(12055L, "第三十六项");
        put(12056L, "第三十七项");
        put(12058L, "第二十八项");


        put(12057L, "第三十八项");
        put(12059L, "第三十九项");
        put(12060L, "第四十项");
        put(12061L, "第四十一项");


        put(12062L, "第四十二项");
        put(12063L, "第四十三项");
        put(12064L, "第四十四项");
        put(12065L, "第四十五项");
        put(12066L, "第四十六项");
        put(12067L, "第四十七项");
        put(12068L, "第四十八项");
        put(12069L, "第四十九项");
        put(12070L, "第五十项");


        put(12071L, "第一项");
        put(12072L, "第二项");
        put(12073L, "第三项");
        put(12074L, "第四项");
        put(12075L, "第五项");


        put(12076L, "第一项");
        put(12077L, "第二项");
        put(12078L, "第三项");
        put(12079L, "第四项");
        put(12080L, "第五项");
        put(12081L, "第六项");
        put(12083L, "第七项");
        put(12084L, "第八项");
        put(12085L, "第九项");
        put(12086L, "第十项");
        put(12087L, "第十一项");
        put(12088L, "第十二项");
        put(12089L, "第十三项");
        put(12090L, "第十四项");
        put(12091L, "第十五项");
        put(12092L, "第十六项");
        put(12093L, "第十七项");
        put(12094L, "第十八项");
        put(12095L, "第十九项");
        put(12096L, "第二十项");
        put(12097L, "第二十一项");
        put(12098L, "第二十二项");
        put(12099L, "第二十三项");
        put(12100L, "第二十四项");
        put(12101L, "第二十五项");
        put(12103L, "第二十六项");


        put(12108L, "第一项");
        put(12142L, "第二项");
        put(12168L, "第三项");
        put(12200L, "第四项");
        put(12224L, "第五项");
        put(12250L, "第六项");
        put(12278L, "第七项");
        put(12303L, "第八项");
        put(12336L, "第九项");
        put(12363L, "第十项");
        put(12247L, "第十一项");
        put(12420L, "第十二项");
    }};


    @Override
    public List<SenForms> queryList(SenForms senForms, Integer sqsjFront, Integer sqsjBack, String statusStr) {
        List<String> strings = null;
        if (StringUtils.isNotEmpty(statusStr)) {
            strings = Arrays.asList(statusStr.split(","));
        }
        if (StringUtils.isNotEmpty(senForms.getPingnames())) {
            senForms.setPingnames(senForms.getPingnames().stream().map(pingname -> pingname.replaceAll("\"", "").replaceAll("\\[", "").replaceAll("\\]", "")).collect(Collectors.toList()));
        }
        List<SenForms> result = this.baseMapper.selectSenFormsList(senForms, sqsjFront, sqsjBack, strings);
        if (CollectionUtil.isNotEmpty(result)) {
            List<Long> ids = result.stream().map(SenForms::getId).collect(Collectors.toList());
            List<SenActionlog> list = senActionlogMapper.selectByFormId(ids);
            List<SenFormsRemark> remarkList = senFormsRemarkMapper.senFormsRemarkMapper(ids);
            Map<Long, Long> collect = null;
            Map<Long, String> collectRemark = null;
            if (CollectionUtil.isNotEmpty(list)) {
                collect = list.stream().collect(Collectors.toMap(SenActionlog::getFormid, SenActionlog::getId));
            }
            if (CollectionUtil.isNotEmpty(remarkList)) {
                collectRemark = remarkList.stream().collect(Collectors.toMap(SenFormsRemark::getFid, SenFormsRemark::getRemark));
            }
            for (SenForms forms : result) {
                if (CollectionUtil.isNotEmpty(collect)) {
                    Long aLong = collect.get(forms.getId());
                    if (aLong != null) {
                        forms.setLogTotal(aLong.intValue());
                    } else {
                        forms.setLogTotal(0);
                    }
                }
                setImage(forms);
                if (CollectionUtil.isNotEmpty(collectRemark)) {
                    String remark = collectRemark.get(forms.getId());
                    if (StringUtils.isNotEmpty(remark)) {
                        forms.setIsRemark(remark);
                    }
                }
            }
        }
        return result;
    }

    @Override
    public PageValue queryList2(SenForms senForms, Integer front, Integer blck, Integer pageNum, Integer pageSize) {
        return null;
    }

    public static boolean fileIExists(String url) throws Exception {
        URL serverUrl = new URL(url);
        HttpURLConnection urlcon = (HttpURLConnection) serverUrl.openConnection();
        Long TotalSize = Long.parseLong(urlcon.getHeaderField("Content-Length"));
        if (TotalSize > 0) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

    public static void setImage(SenForms senForms) {
        if (senForms == null) {
            return;
        }
        if (StringUtils.isNotEmpty(senForms.getImage1()) && StringUtils.isNotEmpty(senForms.getImage1().trim())) {
            if (!senForms.getImage1().contains("http")) {
                if (senForms.getImage1().startsWith("/")) {
                    File file = new File(UploadFile.exist + senForms.getImage1());
                    if (file.exists()) {
                        senForms.setImage1(UploadFile.fileUrl + senForms.getImage1());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + senForms.getImage1());
                            if (file2.exists()) {
                                senForms.setImage1(UploadFile.fileUrl2 + senForms.getImage1());
                            } else {
                                senForms.setImage1(UploadFile.examineUrl + senForms.getImage1());
                            }
                        } else {
                            senForms.setImage1(UploadFile.examineUrl + senForms.getImage1());
                        }
                    }
                } else {
                    File file = new File(UploadFile.exist + "/" + senForms.getImage1());
                    if (file.exists()) {
                        senForms.setImage1(UploadFile.fileUrl + "/" + senForms.getImage1());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + "/" + senForms.getImage1());
                            if (file2.exists()) {
                                senForms.setImage1(UploadFile.fileUrl2 + "/" + senForms.getImage1());
                            } else {
                                senForms.setImage1(UploadFile.examineUrl + "/" + senForms.getImage1());
                            }
                        } else {
                            senForms.setImage1(UploadFile.examineUrl + "/" + senForms.getImage1());
                        }
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(senForms.getImage2()) && StringUtils.isNotEmpty(senForms.getImage2().trim())) {
            if (!senForms.getImage2().contains("http")) {
                if (senForms.getImage2().startsWith("/")) {
                    File file = new File(UploadFile.exist + senForms.getImage2());
                    if (file.exists()) {
                        senForms.setImage2(UploadFile.fileUrl + senForms.getImage2());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + senForms.getImage2());
                            if (file2.exists()) {
                                senForms.setImage2(UploadFile.fileUrl2 + senForms.getImage2());
                            } else {
                                senForms.setImage2(UploadFile.examineUrl + senForms.getImage2());
                            }
                        } else {
                            senForms.setImage2(UploadFile.examineUrl + senForms.getImage2());
                        }
                    }
                } else {
                    File file = new File(UploadFile.exist + "/" + senForms.getImage2());
                    if (file.exists()) {
                        senForms.setImage2(UploadFile.fileUrl + "/" + senForms.getImage2());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + "/" + senForms.getImage2());
                            if (file2.exists()) {
                                senForms.setImage2(UploadFile.fileUrl2 + "/" + senForms.getImage2());
                            } else {
                                senForms.setImage2(UploadFile.examineUrl + "/" + senForms.getImage2());
                            }
                        } else {
                            senForms.setImage2(UploadFile.examineUrl + "/" + senForms.getImage2());
                        }
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(senForms.getImage3()) && StringUtils.isNotEmpty(senForms.getImage3().trim())) {
            if (!senForms.getImage3().contains("http")) {
                if (senForms.getImage3().startsWith("/")) {
                    File file = new File(UploadFile.exist + senForms.getImage3());
                    if (file.exists()) {
                        senForms.setImage3(UploadFile.fileUrl + senForms.getImage3());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + senForms.getImage3());
                            if (file2.exists()) {
                                senForms.setImage3(UploadFile.fileUrl2 + senForms.getImage3());
                            } else {
                                senForms.setImage3(UploadFile.examineUrl + senForms.getImage3());
                            }
                        } else {
                            senForms.setImage3(UploadFile.examineUrl + senForms.getImage3());
                        }
                    }
                } else {
                    File file = new File(UploadFile.exist + "/" + senForms.getImage3());
                    if (file.exists()) {
                        senForms.setImage3(UploadFile.fileUrl + "/" + senForms.getImage3());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + "/" + senForms.getImage3());
                            if (file2.exists()) {
                                senForms.setImage3(UploadFile.fileUrl2 + "/" + senForms.getImage3());
                            } else {
                                senForms.setImage3(UploadFile.examineUrl + "/" + senForms.getImage3());
                            }
                        } else {
                            senForms.setImage3(UploadFile.examineUrl + "/" + senForms.getImage3());
                        }
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(senForms.getImage4()) && StringUtils.isNotEmpty(senForms.getImage4().trim())) {

            if (!senForms.getImage4().contains("http")) {
                if (senForms.getImage4().startsWith("/")) {
                    File file = new File(UploadFile.exist + senForms.getImage4());
                    if (file.exists()) {
                        senForms.setImage4(UploadFile.fileUrl + senForms.getImage4());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + senForms.getImage4());
                            if (file2.exists()) {
                                senForms.setImage4(UploadFile.fileUrl2 + senForms.getImage4());
                            } else {
                                senForms.setImage4(UploadFile.examineUrl + senForms.getImage4());
                            }
                        } else {
                            senForms.setImage4(UploadFile.examineUrl + senForms.getImage4());
                        }
                    }
                } else {
                    File file = new File(UploadFile.exist + "/" + senForms.getImage4());
                    if (file.exists()) {
                        senForms.setImage4(UploadFile.fileUrl + "/" + senForms.getImage4());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + "/" + senForms.getImage4());
                            if (file2.exists()) {
                                senForms.setImage4(UploadFile.fileUrl2 + "/" + senForms.getImage4());
                            } else {
                                senForms.setImage4(UploadFile.examineUrl + "/" + senForms.getImage4());
                            }
                        } else {
                            senForms.setImage4(UploadFile.examineUrl + "/" + senForms.getImage4());
                        }
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(senForms.getImage5()) && StringUtils.isNotEmpty(senForms.getImage5().trim())) {
            if (!senForms.getImage5().contains("http")) {
                if (senForms.getImage5().startsWith("/")) {
                    File file = new File(UploadFile.exist + senForms.getImage5());
                    if (file.exists()) {
                        senForms.setImage5(UploadFile.fileUrl + senForms.getImage5());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + senForms.getImage5());
                            if (file2.exists()) {
                                senForms.setImage5(UploadFile.fileUrl2 + senForms.getImage5());
                            } else {
                                senForms.setImage5(UploadFile.examineUrl + senForms.getImage5());
                            }
                        } else {
                            senForms.setImage5(UploadFile.examineUrl + senForms.getImage5());
                        }
                    }
                } else {
                    File file = new File(UploadFile.exist + "/" + senForms.getImage5());
                    if (file.exists()) {
                        senForms.setImage5(UploadFile.fileUrl + "/" + senForms.getImage5());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + "/" + senForms.getImage5());
                            if (file2.exists()) {
                                senForms.setImage5(UploadFile.fileUrl2 + "/" + senForms.getImage5());
                            } else {
                                senForms.setImage5(UploadFile.examineUrl + "/" + senForms.getImage5());
                            }
                        } else {
                            senForms.setImage5(UploadFile.examineUrl + "/" + senForms.getImage5());
                        }
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(senForms.getImage6()) && StringUtils.isNotEmpty(senForms.getImage6().trim())) {
            if (!senForms.getImage6().contains("http")) {
                if (senForms.getImage6().startsWith("/")) {
                    File file = new File(UploadFile.exist + senForms.getImage6());
                    if (file.exists()) {
                        senForms.setImage6(UploadFile.fileUrl + senForms.getImage6());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + senForms.getImage6());
                            if (file2.exists()) {
                                senForms.setImage6(UploadFile.fileUrl2 + senForms.getImage6());
                            } else {
                                senForms.setImage6(UploadFile.examineUrl + senForms.getImage6());
                            }
                        } else {
                            senForms.setImage6(UploadFile.examineUrl + senForms.getImage6());
                        }
                    }
                } else {
                    File file = new File(UploadFile.exist + "/" + senForms.getImage6());
                    if (file.exists()) {
                        senForms.setImage6(UploadFile.fileUrl + "/" + senForms.getImage6());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + "/" + senForms.getImage6());
                            if (file2.exists()) {
                                senForms.setImage6(UploadFile.fileUrl2 + "/" + senForms.getImage6());
                            } else {
                                senForms.setImage6(UploadFile.examineUrl + "/" + senForms.getImage6());
                            }
                        } else {
                            senForms.setImage6(UploadFile.examineUrl + "/" + senForms.getImage6());
                        }
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(senForms.getQianzi()) && StringUtils.isNotEmpty(senForms.getQianzi().trim())) {
            if (!senForms.getQianzi().contains("http")) {
                if (senForms.getQianzi().startsWith("/")) {
                    File file = new File(UploadFile.exist + senForms.getQianzi());
                    if (file.exists()) {
                        senForms.setQianzi(UploadFile.fileUrl + senForms.getQianzi());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + senForms.getQianzi());
                            if (file2.exists()) {
                                senForms.setQianzi(UploadFile.fileUrl2 + senForms.getQianzi());
                            } else {
                                senForms.setQianzi(UploadFile.examineUrl + senForms.getQianzi());
                            }
                        } else {
                            senForms.setQianzi(UploadFile.examineUrl + senForms.getQianzi());
                        }
                    }
                } else {
                    File file = new File(UploadFile.exist + "/" + senForms.getQianzi());
                    if (file.exists()) {
                        senForms.setQianzi(UploadFile.fileUrl + "/" + senForms.getQianzi());
                    } else {
                        if (StringUtils.isNotEmpty(UploadFile.exist2)) {
                            File file2 = new File(UploadFile.exist2 + "/" + senForms.getQianzi());
                            if (file2.exists()) {
                                senForms.setQianzi(UploadFile.fileUrl2 + "/" + senForms.getQianzi());
                            } else {
                                senForms.setQianzi(UploadFile.examineUrl + "/" + senForms.getQianzi());
                            }
                        } else {
                            senForms.setQianzi(UploadFile.examineUrl + "/" + senForms.getQianzi());
                        }
                    }
                }
            }
        }
    }

    @Override
    public Boolean updateImageById(JSONObject jsonObject) {
        return this.baseMapper.updateImageById(jsonObject) == 1;
    }

    @Override
    public SendOrderVO lead(MultipartFile multipartFile) {
        SendOrderVO sendOrderVO = new SendOrderVO();
        InitStockDataHandle initStockDataHandle = new InitStockDataHandle();
        try {
            EasyExcel.read(multipartFile.getInputStream(), SenFormsExcel.class, initStockDataHandle).doReadAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (CollectionUtil.isEmpty(initStockDataHandle.getValue())) {
            return sendOrderVO;
        }
        sendOrderVO.setTotal(initStockDataHandle.getValue().size());
        List<String> sqhList = initStockDataHandle.getValue().stream().map(SenFormsExcel::getSqh).collect(Collectors.toList()).stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        LambdaQueryWrapper<SenForms> senFormsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        senFormsLambdaQueryWrapper.select(SenForms::getSqh);
        senFormsLambdaQueryWrapper.in(SenForms::getSqh, sqhList);
        List<SenForms> exitSQH = this.baseMapper.selectList(senFormsLambdaQueryWrapper);
        Map<String, String> map = null;
        if (CollectionUtil.isNotEmpty(exitSQH)) {
            map = exitSQH.stream().collect(Collectors.toMap(SenForms::getSqh, SenForms::getSqh, (k, v) -> k));
        }
        List<JSONObject> senStreets = senStreetsMapper.selectByIdsAll();
        Map<String, JSONObject> collect = new HashMap<>();
        List<JSONObject> distinct = new ArrayList<>();
        for (JSONObject senStreet : senStreets) {
            String title = senStreet.getString("title");
            boolean b = collect.containsKey(title);
            if (b) {
                distinct.add(senStreet);
            } else {
                collect.put(title, senStreet);
            }
        }
        List<SenFormsExcel> distinctValue = initStockDataHandle.getValue().stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(data -> data.getSqh()))), ArrayList::new));
        final Map<String, String> finalMap = map;
        final List<String> list = new ArrayList<>();
        List<SenForms> insertList = distinctValue.stream().map(a -> {
            if (CollectionUtil.isNotEmpty(finalMap)) {
                String s = finalMap.get(a.getSqh());
                if (StringUtils.isNotEmpty(s)) {
                    return null;
                }
            }
            SenForms senForms = new SenForms();
            BeanUtil.copyProperties(a, senForms);
            if (null != a.getSblx()) {
                senForms.setSblx(a.getSblx());
            } else {
                senForms.setSblx(1);
            }
            if (StringUtils.isNotEmpty(a.getZzjd())) {
                JSONObject jsonObject = collect.get(a.getZzjd());
                if (CollectionUtil.isNotEmpty(jsonObject)) {
                    if (StringUtils.isEmpty(jsonObject.getString("pid"))) {
                        Long id = jsonObject.getLong("id");
                        if (id != null && id != 0L) {
                            senForms.setStreetid(id);
                        } else {
                            list.add(a.getSqh());
                            return null;
                        }
                    } else {
                        for (JSONObject object : distinct) {
                            String title = object.getString("title");
                            if (title.equals(a.getZzjd())) {
                                String pid = object.getString("pid");
                                if (StringUtils.isEmpty(pid)) {
                                    Long id = object.getLong("id");
                                    if (id != null && id != 0L) {
                                        senForms.setStreetid(id);
                                    }
//                                    else {
//                                        list.add(a.getSqh());
//                                        return null;
//                                    }
                                }
                            }
                        }
                    }
                }
                if(senForms.getStreetid() == null){
                    JSONObject jsonObject2 = collect.get(a.getZzjd() + "路");
                    if (CollectionUtil.isNotEmpty(jsonObject2) && StringUtils.isEmpty(jsonObject2.getString("pid"))) {
                        Long id = jsonObject2.getLong("id");
                        if (id != null && id != 0L) {
                            senForms.setStreetid(id);
                        } else {
                            list.add(a.getSqh());
                            return null;
                        }
                    } else {
                        list.add(a.getSqh());
                        return null;
                    }
                }
            }
            if (StringUtils.isNotEmpty(a.getZzjw())) {
                JSONObject jsonObject = collect.get(a.getZzjw());
                JSONObject jsonObjects = collect.get(a.getZzjd());
                if (CollectionUtil.isEmpty(jsonObjects) || jsonObjects.getInteger("pid") != null) {
                    jsonObjects = collect.get(a.getZzjd() + "路");
                }
                if (CollectionUtil.isNotEmpty(jsonObject) && jsonObject.getInteger("pid") != null && jsonObjects.getInteger("id").equals(jsonObject.getInteger("pid"))) {
                    Long id = jsonObject.getLong("id");
                    if (id != null && id != 0L) {
                        senForms.setJuweiId(id);
                    } else {
                        list.add(a.getSqh());
                        return null;
                    }
                } else {
                    list.add(a.getSqh());
                    return null;
                }
            }
            if (senForms.getStreetid() == null || senForms.getJuweiId() == null) {
                return null;
            }
            senForms.setPglx(new Long(a.getPtype()));
            if (StringUtils.isNotEmpty(a.getSfzh())) {
                if (StringUtils.isNotEmpty(a.getSfzh()) && a.getSfzh().length() > 14) {
                    try {
                        boolean integer = NumberUtil.isInteger(a.getSfzh().substring(6, 14));
                        if (integer) {
                            senForms.setCsny(new Long(a.getSfzh().substring(6, 14)));
                        }
                    } catch (Exception e) {
                        senForms.setCsny(null);
                    }
                }
            }

            if (StringUtils.isNotEmpty(a.getSldate())) {
                senForms.setSldate(DateUtil.parse(a.getSldate(), "yyyyMMdd").getTime() / 1000);
            }
            senForms.setDtime(System.currentTimeMillis() / 1000);
            return senForms;
        }).collect(Collectors.toList()).stream().filter(ObjectUtils::isNotEmpty).collect(Collectors.toList());
        for (SenForms senForms : insertList) {
            try {
                DateTime yyyyMMdd = DateUtil.parse(senForms.getSqh().substring(9, 17), "yyyyMMdd");
                senForms.setSqsj(DateUtil.format(yyyyMMdd, "yyyy-MM-dd"));
            } catch (Exception e) {
                senForms.setSqsj(DateUtil.format(new Date(), "yyyy-MM-dd"));
            }
            if (save(senForms)) {
                sendOrderVO.setSuccess(sendOrderVO.getSuccess() + 1);
            }
        }
        sendOrderVO.setDistinct(list);
        sendOrderVO.setFailure(sendOrderVO.getTotal() - sendOrderVO.getSuccess());
        return sendOrderVO;
    }

    /**
     * DOTO 后期需要对接 微信公众号消息推送模板
     * *
     */
    @Override
    public SendOrderVO sendOrders(MultipartFile multipartFile) {
        SendOrderVO sendOrderVO = new SendOrderVO();
        FormsSendOrdersDataHandle formsSendOrdersDataHandle = new FormsSendOrdersDataHandle();
        try {
            EasyExcel.read(multipartFile.getInputStream(), SenFormsSendOrders.class, formsSendOrdersDataHandle).doReadAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (CollectionUtil.isEmpty(formsSendOrdersDataHandle.getValue())) {
            return sendOrderVO;
        }
        sendOrderVO.setTotal(formsSendOrdersDataHandle.getValue().size());
        List<SenFormsSendOrders> distinctValue = formsSendOrdersDataHandle.getValue().stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(data -> data.getSqh()))), ArrayList::new));
        List<SenPgy> senPgies = senPgyMapper.selectAllPgy();
        List<String> distict = new ArrayList<>();
        Map<String, SenPgy> collect = senPgies.stream().filter(a -> a.getId() != null && StringUtils.isNotEmpty(a.getXm())).collect(Collectors.toList()).stream().collect(Collectors.toMap(SenPgy::getXm, a -> a,
                (item1, item2) -> item1));
        List<SenForms> updateList = distinctValue.stream().filter(a -> StringUtils.isNotEmpty(a.getSqh())).collect(Collectors.toList()).stream().filter(a -> StringUtils.isNotEmpty(a.getPgy1()) && StringUtils.isNotEmpty(a.getPgy2())).collect(Collectors.toList()).stream().map(a -> {
            SenForms senForms = new SenForms();
            senForms.setSqh(a.getSqh());
            String pingname = "";
            String pingUid = "";
            if (StringUtils.isNotEmpty(a.getPgy1())) {
                if (StringUtils.isNotEmpty(a.getPgy1())) {
                    SenPgy aLong = collect.get(a.getPgy1());
                    if (aLong != null) {
                        if (aLong.getDel() > 1) {
                            distict.add(a.getSqh());
                            return null;
                        } else {
                            pingname = a.getPgy1();
                            pingUid = aLong.getId().toString();
                        }
                    } else {
                        distict.add(a.getSqh());
                        return null;
                    }
                }
            }
            if (StringUtils.isNotEmpty(a.getPgy2())) {
                SenPgy aLong = collect.get(a.getPgy2());
                if (aLong != null) {
                    if (aLong.getDel() > 1) {
                        distict.add(a.getSqh());
                        return null;
                    } else {
                        if (StringUtils.isNotEmpty(pingname)) {
                            pingname = pingname + "," + a.getPgy2();
                        } else {
                            pingname = a.getPgy2();
                        }
                        if (StringUtils.isNotEmpty(pingUid)) {
                            pingUid = pingUid + "," + aLong.getId();
                        } else {
                            pingUid = aLong.getId().toString();
                        }
                    }
                } else {
                    distict.add(a.getSqh());
                    return null;
                }
            }
            if ((StringUtils.isEmpty(pingname) || StringUtils.isEmpty(pingUid))) {
                return null;
            } else {
                senForms.setPingname(pingname);
                senForms.setPinguid(pingUid);
                return senForms;
            }
        }).collect(Collectors.toList()).stream().filter(ObjectUtils::isNotEmpty).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(updateList)) {
            sendOrderVO.setFailure(sendOrderVO.getTotal());
            return sendOrderVO;
        }
        for (SenForms senForms : updateList) {
            LambdaUpdateWrapper<SenForms> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(SenForms::getSqh, senForms.getSqh());
            lambdaUpdateWrapper.set(SenForms::getPingname, senForms.getPingname());
            lambdaUpdateWrapper.set(SenForms::getPinguid, senForms.getPinguid());
            lambdaUpdateWrapper.set(SenForms::getPaidanTime, System.currentTimeMillis() / 1000);
            lambdaUpdateWrapper.set(SenForms::getIspai, 1);
            if (update(lambdaUpdateWrapper)) {
                sendOrderVO.setSuccess(sendOrderVO.getSuccess() + 1);
            }
        }
        sendOrderVO.setDistinct(distict);
        sendOrderVO.setFailure(sendOrderVO.getTotal() - sendOrderVO.getSuccess());
        return sendOrderVO;
    }

    @Override
    public SendOrderVO updateLevelExcel(MultipartFile multipartFile) {
        SendOrderVO sendOrderVO = new SendOrderVO();
        FormsUpdateLevelDataHandle formsSendOrdersDataHandle = new FormsUpdateLevelDataHandle();
        try {
            EasyExcel.read(multipartFile.getInputStream(), SenUpdateLevelExcel.class, formsSendOrdersDataHandle).doReadAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (CollectionUtil.isEmpty(formsSendOrdersDataHandle.getValue())) {
            return sendOrderVO;
        }
        sendOrderVO.setTotal(formsSendOrdersDataHandle.getValue().size());
        List<SenPgy> senPgies = senPgyMapper.selectAllPgy();
        List<String> distict = new ArrayList<>();
        Map<String, SenPgy> collect = senPgies.stream().filter(a -> a.getId() != null && StringUtils.isNotEmpty(a.getXm())).collect(Collectors.toList()).stream().collect(Collectors.toMap(SenPgy::getXm, a -> a,
                (item1, item2) -> item1));
        List<SenForms> updateList = formsSendOrdersDataHandle.getValue().stream().filter(a -> StringUtils.isNotEmpty(a.getSqh())).collect(Collectors.toList()).stream().collect(Collectors.toList()).stream().map(a -> {
            SenForms senForms = new SenForms();
            senForms.setSqh(a.getSqh());
            String pingname = "";
            String pingUid = "";
            if (StringUtils.isNotEmpty(a.getPgy1())) {
                if (StringUtils.isNotEmpty(a.getPgy1())) {
                    SenPgy aLong = collect.get(a.getPgy1());
                    if (aLong != null) {
                        if (aLong.getDel() > 1) {
                            distict.add(a.getSqh());
                            return null;
                        } else {
                            pingname = a.getPgy1();
                            pingUid = aLong.getId().toString();
                        }
                    } else {
                        distict.add(a.getSqh());
                        return null;
                    }
                }
            }
            if (StringUtils.isNotEmpty(a.getPgy2())) {
                SenPgy aLong = collect.get(a.getPgy2());
                if (aLong != null) {
                    if (aLong.getDel() > 1) {
                        distict.add(a.getSqh());
                        return null;
                    } else {
                        if (StringUtils.isNotEmpty(pingname)) {
                            pingname = pingname + "," + a.getPgy2();
                        } else {
                            pingname = a.getPgy2();
                        }
                        if (StringUtils.isNotEmpty(pingUid)) {
                            pingUid = pingUid + "," + aLong.getId();
                        } else {
                            pingUid = aLong.getId().toString();
                        }
                    }
                } else {
                    distict.add(a.getSqh());
                    return null;
                }
            }

            if (StringUtils.isNotEmpty(a.getCloseAccount())) {
                if (a.getCloseAccount().equals("是")) {
                    senForms.setJiesuan(1);
                } else if (a.getCloseAccount().equals("否")) {
                    senForms.setJiesuan(0);
                }
            }
            if (StringUtils.isNotEmpty(a.getDate())) {
                senForms.setOrderDate(new Long(a.getDate()));
            }
            if (a.getLevel() != null) {
                senForms.setPgdj(a.getLevel() > 6 ? 6 : a.getLevel());
            }
            if ((StringUtils.isEmpty(pingname) || StringUtils.isEmpty(pingUid))) {
                return null;
            } else {
                senForms.setPingname(pingname);
                senForms.setPinguid(pingUid);
                return senForms;
            }
        }).collect(Collectors.toList()).stream().filter(ObjectUtils::isNotEmpty).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(updateList)) {
            return sendOrderVO;
        }
        for (SenForms senForms : updateList) {
            LambdaUpdateWrapper<SenForms> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(SenForms::getSqh, senForms.getSqh());
            lambdaUpdateWrapper.set(SenForms::getPgdjStatus, 1);
            if (StringUtils.isNotEmpty(senForms.getPingname())) {
                lambdaUpdateWrapper.set(SenForms::getPingname, senForms.getPingname());
            }
            if (StringUtils.isNotEmpty(senForms.getPinguid())) {
                lambdaUpdateWrapper.set(SenForms::getPinguid, senForms.getPinguid());
            }
            if (senForms.getPgdj() != null) {
                lambdaUpdateWrapper.set(SenForms::getPgdj, senForms.getPgdj());
            }
            if (senForms.getJiesuan() != null) {
                lambdaUpdateWrapper.set(SenForms::getJiesuan, senForms.getJiesuan());
            }
            if (senForms.getOrderDate() != null) {
                lambdaUpdateWrapper.set(SenForms::getOrderDate, senForms.getOrderDate());
            }
            if (update(lambdaUpdateWrapper)) {
                sendOrderVO.setSuccess(sendOrderVO.getSuccess() + 1);
            }
        }
        sendOrderVO.setFailure(sendOrderVO.getSuccess() == 0 ? sendOrderVO.getTotal() : sendOrderVO.getTotal() - sendOrderVO.getSuccess());
        return sendOrderVO;
    }

    @Override
    public void formsDownloadExcel(HttpServletResponse response, SenForms senForms, Integer front, Integer
            blck, String statusStr) {
        List<SenFormsDownloadExcel> senFormsDownloadExcels = this.baseMapper.formsDownloadExcel(senForms, front, blck, StringUtils.isNotEmpty(statusStr) ? Arrays.asList(statusStr.split(",")) : null);
        if (CollectionUtil.isEmpty(senFormsDownloadExcels)) {
            return;
        }
        List<Long> collect = senFormsDownloadExcels.stream().map(SenFormsDownloadExcel::getId).collect(Collectors.toList());
        List<SenFormsRemark> remarks = null;
        if (CollectionUtil.isNotEmpty(collect)) {
            remarks = senFormsRemarkMapper.selectNewestRemark(collect);
        }
        Map<Long, String> remarkMap = null;
        if (CollectionUtil.isNotEmpty(remarks)) {
            remarkMap = remarks.stream().collect(Collectors.toMap(SenFormsRemark::getFid, SenFormsRemark::getRemark, (item1, item2) -> item1));
        }
        for (int i = 0; i < senFormsDownloadExcels.size(); i++) {
            senFormsDownloadExcels.get(i).setIndex(i + 1);
            if (StringUtils.isNotEmpty(senFormsDownloadExcels.get(i).getSubmtime()) && !senFormsDownloadExcels.get(i).getSubmtime().equals("0")) {
                senFormsDownloadExcels.get(i).setSubmtime(DateUtil.format(new Date(new Long(senFormsDownloadExcels.get(i).getSubmtime()) * 1000L), "yyyy-MM-dd HH:mm:ss"));
            } else {
                senFormsDownloadExcels.get(i).setSubmtime("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getBq())) {
                senFormsDownloadExcels.get(i).setBq("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getXh())) {
                senFormsDownloadExcels.get(i).setXh("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getPgyA())) {
                senFormsDownloadExcels.get(i).setPgyA("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getPgyB())) {
                senFormsDownloadExcels.get(i).setPgyB("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getYpgdj())) {
                senFormsDownloadExcels.get(i).setYpgdj("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getRq())) {
                senFormsDownloadExcels.get(i).setRq("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getBz())) {
                senFormsDownloadExcels.get(i).setBz("-");
            }
            if (CollectionUtil.isNotEmpty(remarkMap)) {
                String s = remarkMap.get(senFormsDownloadExcels.get(i).getId());
                if (StringUtils.isNotEmpty(s)) {
                    senFormsDownloadExcels.get(i).setBz(s);
                }
            }
            if (StringUtils.isNotEmpty(senFormsDownloadExcels.get(i).getNl()) && !senFormsDownloadExcels.get(i).getNl().equals("-")) {
                Integer integer = new Integer(senFormsDownloadExcels.get(i).getNl());
                if (integer > 150) {
                    senFormsDownloadExcels.get(i).setNl("-");
                }
                if (integer < 0) {
                    senFormsDownloadExcels.get(i).setNl("-");
                }
            }
        }
        try {
            EasyExcelUtils.writeForWeb(response, "123456", SenFormsDownloadExcel.class, senFormsDownloadExcels, "评估结果");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void formsDownloadExcel2(HttpServletResponse response, SenForms senForms, Integer front, Integer
            blck, String statusStr) {
        List<SenFormsDownloadExcel2> senFormsDownloadExcels = this.baseMapper.formsDownloadExcel2(senForms, front, blck, StringUtils.isNotEmpty(statusStr) ? Arrays.asList(statusStr.split(",")) : null);
        if (CollectionUtil.isEmpty(senFormsDownloadExcels)) {
            return;
        }
        List<Long> collect = senFormsDownloadExcels.stream().map(SenFormsDownloadExcel2::getId).collect(Collectors.toList());
        List<SenFormsRemark> remarks = null;
        if (CollectionUtil.isNotEmpty(collect)) {
            remarks = senFormsRemarkMapper.selectNewestRemark(collect);
        }
        Map<Long, String> remarkMap = null;
        if (CollectionUtil.isNotEmpty(remarks)) {
            remarkMap = remarks.stream().collect(Collectors.toMap(SenFormsRemark::getFid, SenFormsRemark::getRemark, (item1, item2) -> item1));
        }
        for (int i = 0; i < senFormsDownloadExcels.size(); i++) {
            senFormsDownloadExcels.get(i).setIndex(i + 1);
            if (StringUtils.isNotEmpty(senFormsDownloadExcels.get(i).getSuctime()) && !senFormsDownloadExcels.get(i).getSuctime().equals("0")) {
                senFormsDownloadExcels.get(i).setSuctime(DateUtil.format(new Date(new Long(senFormsDownloadExcels.get(i).getSuctime()) * 1000), "yyyy-MM-dd"));
            } else {
                senFormsDownloadExcels.get(i).setSuctime("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getBq())) {
                senFormsDownloadExcels.get(i).setBq("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getXh())) {
                senFormsDownloadExcels.get(i).setXh("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getPgyA())) {
                senFormsDownloadExcels.get(i).setPgyA("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getCancelreason())) {
                senFormsDownloadExcels.get(i).setCancelreason("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getPgyB())) {
                senFormsDownloadExcels.get(i).setPgyB("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getYpgdj())) {
                senFormsDownloadExcels.get(i).setYpgdj("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getRq())) {
                senFormsDownloadExcels.get(i).setRq("-");
            }
            if (StringUtils.isEmpty(senFormsDownloadExcels.get(i).getBz())) {
                senFormsDownloadExcels.get(i).setBz("-");
            }
            if (CollectionUtil.isNotEmpty(remarkMap)) {
                String s = remarkMap.get(senFormsDownloadExcels.get(i).getId());
                if (StringUtils.isNotEmpty(s)) {
                    senFormsDownloadExcels.get(i).setBz(s);
                }
            }
            if (StringUtils.isNotEmpty(senFormsDownloadExcels.get(i).getNl()) && !senFormsDownloadExcels.get(i).getNl().equals("-")) {
                Integer integer = new Integer(senFormsDownloadExcels.get(i).getNl());
                if (integer > 150) {
                    senFormsDownloadExcels.get(i).setNl("-");
                }
                if (integer < 0) {
                    senFormsDownloadExcels.get(i).setNl("-");
                }
            }
        }
        try {
            EasyExcelUtils.writeForWeb(response, "123456", SenFormsDownloadExcel2.class, senFormsDownloadExcels, "评估结果");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void formsDownloadValueExcel(HttpServletResponse response, Long id) {
        String juzhudizhi = null;
        String sangeyue = null;
        String shieryue = null;
        String sangeyue2 = null;
        Integer juzhudizhiIndex = null;
        Integer sangeyueIndex = null;
        Integer shieryueIndex = null;
        Integer sangeyue2Index = null;
        SenForms senForms = senFormsMapper.selectById(id);
        List<SenFormFieldsValue> result = new ArrayList<>();
        if (senForms != null) {
            result.add(new SenFormFieldsValue("申请号", senForms.getSqh()));
            result.add(new SenFormFieldsValue("评估员", senForms.getPingname()));
            result.add(new SenFormFieldsValue("评估类型", senForms.getStatus() == 1 ? "初次评估" : senForms.getStatus() == 2 ? "复核评估" : senForms.getStatus() == 3 ? "状态评估" : senForms.getStatus() == 4 ? "期末评估" : senForms.getStatus() == 5 ? "绿色通道" : senForms.getStatus() == 6 ? "终核评估" : null));
            result.add(new SenFormFieldsValue("所属街道", senForms.getStreetid() != null && senForms.getStreetid() != 0 ? senStreetsMapper.selectById(senForms.getStreetid()).getTitle() : ""));
            result.add(new SenFormFieldsValue("所属居委", senForms.getJuweiId() != null && senForms.getJuweiId() != 0 ? senStreetsMapper.selectById(senForms.getJuweiId()).getTitle() : ""));
            result.add(new SenFormFieldsValue("评估地点", senForms.getSxdz()));
            result.add(new SenFormFieldsValue("评估等级", senForms.getPgdj() != null ? senForms.getPgdj().toString() : "-"));
            result.add(new SenFormFieldsValue("评估完成时间", senForms.getSuctime() != null && senForms.getSuctime() != 0 ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(senForms.getSuctime() * 1000L)) : "-"));
            if (senForms.getStatus() == 3 || senForms.getStatus() == 2) {
                result.add(new SenFormFieldsValue("取消原因", senForms.getCancelreason()));
            }
        }
        List<SenFormFieldsValue> list = senFormsMapper.formsDownloadValueExcel(id);
        List<SenFormFieldsValue> collect = new ArrayList<>();
        for (SenFormFieldsValue a : list) {
            a.setXh(xuhao.get(a.getId()));
            String sa = value.get(a.getId());
            if (StringUtils.isNotEmpty(sa)) {
                a.setType(sa);
            } else {
                a.setType("特定疾病信息");
            }
            if (a.getId() == 11666L) {
                a.setQt("cm");
            }
            if (a.getId() == 11667L) {
                a.setQt("kg");
            }
            if (StringUtils.isNotEmpty(a.getTitle()) && (a.getTitle().contains("在过去的12个月") || a.getTitle().contains("居住地址") || a.getTitle().contains("居住楼层"))) {
                String s = value.get(a.getId());
                if (StringUtils.isNotEmpty(s)) {
                    a.setType(s);
                } else {
                    a.setType("疾病总体信息");
                }
                boolean contains = a.getSets().contains("是##否");
                String value = a.getSets().replaceAll("\\|", "").replaceAll("\\d", "").replaceAll("number", "");
                if (contains) {
                    value.replaceAll("是##否", "");
                    if (a.getVal().equals("1")) {
                        value.replaceAll("\\$", "是");
                    } else if (a.getVal().equals("2")) {
                        value.replaceAll("\\$", "否");
                    }
                } else {
                    value = value.replaceAll("\\$", a.getVal());
                    if (a.getTitle().contains("在过去的12个月")) {
                        if (StringUtils.isNotEmpty(shieryue)) {
                            shieryue = shieryue + "" + value;
                        } else {
                            shieryueIndex = collect.size();
                            shieryue = value;
                            a.setXh("第三十七项");
                            a.setVal(shieryue);
                            collect.add(a);
                            continue;
                        }
                    } else if (a.getTitle().contains("居住地址")) {
                        if (StringUtils.isNotEmpty(juzhudizhi)) {
                            juzhudizhi = juzhudizhi + "" + value;
                        } else {
                            juzhudizhiIndex = collect.size();
                            juzhudizhi = value;
                            a.setVal(juzhudizhi);
                            a.setXh("第十三项");
                            collect.add(a);
                            continue;
                        }
                    } else if (a.getTitle().contains("居住楼层")) {
                        if (StringUtils.isNotEmpty(juzhudizhi)) {
                            juzhudizhi = juzhudizhi + "" + value;
                        } else {
                            juzhudizhiIndex = collect.size();
                            juzhudizhi = value;
                            a.setVal(juzhudizhi);
                            a.setXh("第二十八项");
                            collect.add(a);
                            continue;
                        }
                    } else {
                        if (StringUtils.isNotEmpty(sangeyue)) {
                            sangeyue = sangeyue + "" + value;

                            a.setVal(sangeyue);
                        } else {
                            sangeyueIndex = collect.size();
                            sangeyue = value;
                            a.setVal(sangeyue);
                            collect.add(a);
                            continue;
                        }
                    }
                }
            } else {
                if (StringUtils.isNotEmpty(a.getSets()) && a.getSets().contains("##") && StringUtils.isNotEmpty(a.getVal())) {
                    String[] split = a.getSets().split("##");
                    a.setVal(format(Arrays.asList(split), Arrays.asList(a.getVal().split("\\{s}")).stream().map(Integer::parseInt).collect(Collectors.toList())));
                }
                collect.add(a);
            }
        }
        if (juzhudizhiIndex != null) {
            collect.get(juzhudizhiIndex).setVal(juzhudizhi);
        }
        if (sangeyueIndex != null) {
            collect.get(sangeyueIndex).setVal(sangeyue);
        }
        if (shieryueIndex != null) {
            collect.get(shieryueIndex).setVal(shieryue);
        }
        if (sangeyue2Index != null) {
            collect.get(sangeyue2Index).setVal(sangeyue2);
        }
        List<SenFormFieldsValue> collect1 = collect.stream().filter(a -> {
            return StringUtils.isNotEmpty(a.getTitle());
        }).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(collect1)) {
            result.addAll(collect1);
        }
        try {
            EasyExcelUtils.writeForWeb2(response, senForms.getSqh(), SenFormFieldsValue.class, result, senForms.getSqh());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<JSONObject> queryFiltration(String pingname, String statusStr) {
        List<String> list = null;
        if (StringUtils.isNotEmpty(statusStr)) {
            list = Arrays.asList(statusStr.split(","));
        }
        return senFormsMapper.queryFiltration(pingname, list);
    }

    public String format(List<String> list, List<Integer> index) {
        if (CollectionUtil.isEmpty(list) || CollectionUtil.isEmpty(index)) {
            return new String();
        }
        String result = null;
        for (Integer integer : index) {
            if (integer >= list.size()) {
                continue;
            }
            if (StringUtils.isEmpty(result)) {
                result = integer + "." + list.get(integer);
            } else {
                result = result + "," + integer + "." + list.get(integer);
            }
        }
        return result;
    }
}
